package com.bainaeco.framework.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

public class PropertiesUtil {
	private static Map<String, Properties> propertiesMap = new HashMap<String, Properties>();

	public static String getProperties(String path, String key) throws IOException {

		Properties properties = propertiesMap.get(path);

		if (properties == null || properties.isEmpty()) {
			Resource resource = new ClassPathResource(path);
			properties = PropertiesLoaderUtils.loadProperties(resource);

			if (properties == null || properties.isEmpty()) {
				throw new IOException(path + " not found");
			} else {
				propertiesMap.put(path, properties);
			}
		}

		return properties.getProperty(key);
	}

	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
			try {
				System.out.println(PropertiesUtil.getProperties("/key/system", "secret.key"));
				System.out.println(PropertiesUtil.getProperties("/key/system", "aes.password"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
